package com.eastday;

import java.io.IOException;

import org.jdom.JDOMException;

import com.eastday.biz.GenerateHtmlBiz;
import com.eastday.biz.PageNoUpdateBiz;
import com.eastday.util.BeanFactory;
import com.eastday.util.ExceptionHandler;
import com.eastday.util.ParseConfigFactory;

/**
 * 
 * @author dingxs
 *
 */
public class ExportProcess implements Runnable {
	
	private int interval;
	private ExceptionHandler exceptionHandler;
	

	public int getInterval() {
		return interval;
	}



	public void setInterval(int interval) {
		this.interval = interval;
	}

	public ExportProcess(){
		try{
			ParseConfigFactory.constructSysConfig();
			exceptionHandler = new ExceptionHandler();
			interval = ParseConfigFactory.sysConfigModel.getInterval();
		}catch(JDOMException e){
			exceptionHandler.writeToLog(e.getMessage());
		}catch(IOException e){
			exceptionHandler.writeToLog(e.getMessage());
		}
	}
	
	private void pagenate(){
		PageNoUpdateBiz pageNoUpdateBiz = (PageNoUpdateBiz)BeanFactory.getBean("PageNoUpdateBiz");
		pageNoUpdateBiz.updatePageNo();
		exceptionHandler.displayOnConsole("pagenate has finished");
	}
	
	private void exportHtml()
	{
		GenerateHtmlBiz generateHtmlBiz = (GenerateHtmlBiz)BeanFactory.getBean("GenerateHtmlBiz");
		String filename = ParseConfigFactory.sysConfigModel.getExportFilename();
		generateHtmlBiz.printData(filename);
		exceptionHandler.displayOnConsole("export has finished");
	}



	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true)
		{
			try {
				this.pagenate();
				Thread.sleep(3*1000);
				this.exportHtml();
				Thread.sleep(interval*60*1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				exceptionHandler.writeToLog(e.getMessage());
			}
		}
	}

	
}
